package com.usian.article.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.service.AuthorService;
import com.usian.apis.article.AuthorControllerApi;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/v1/author")
public class AuthorController implements AuthorControllerApi {

    @Autowired
    private AuthorService authorService;

    @GetMapping("findByUserId/{id}")
    @Override
    public ApAuthor findByUserId(@PathVariable Integer id) {
        List<ApAuthor> list = authorService.list(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, id));
        if (list!=null&&!list.isEmpty()){
            return list.get(0);

        }        return null;
    }

    @PostMapping("/save")
    @Override
    public ResponseResult saveAuthor(@RequestBody ApAuthor apAuthor) {
        apAuthor.setCreatedTime(new Date());
        authorService.save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /***
     * 文章审核用的 根据自媒体用户名称查询作者
     * @param name
     * @return
     */
    @Override
    @GetMapping("/findByName/{name}")
    public ApAuthor findByName(@PathVariable String name) {

        ApAuthor apAuthor=authorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName,name));
        return apAuthor;
    }

    /**
     * 根据Id查询作者
     * @param id
     * @return
     */
    @Override
    @GetMapping("/one/{id}")
    public ApAuthor findById(@PathVariable Integer id) {
        return authorService.getById(id);
    }
}
